using System;
using System.Windows.Input;
using churchtool.Plugin.SermonUpload.Operations.Commands;
using System.Collections.Generic;
using churchtool.Plugin.SermonUpload.Data;
using churchtool.Plugin.Interfaces.ViewModel;

namespace churchtool.Plugin.SermonUpload.ViewModel
{
    public class UploadWindowViewModel : ViewModelBase, IPageViewModel
    {
        #region Fields

        // Property variables
        private Boolean p_BrowseButtonIsVisible = true;
        private Boolean p_SetID3TagIsVisible = false;
        private Boolean p_UploadButtonIsEnable = false;

        public string p_Preacher;
        public string p_SermonTitle;
        public string p_SermonComment;
        public DateTime p_SermonDate = DateTime.Now;

        #endregion


        #region Events

        public event EventHandler WorkStarted;
        public event EventHandler WorkEnded;

        #endregion

        #region Constructor

        /// <summary>
        /// Default constructor.
        /// </summary>
        public UploadWindowViewModel()
        {
            this.Initialize();
        }

        #endregion

        #region Child View Models

        /// <summary>
        /// Progress dialog view model.
        /// </summary>
        public ProgressDialogViewModel ProgressDialogViewModel { get; set; }

        #endregion

        #region Command Properties

        /// <summary>
        /// Executes the demo operation.
        /// </summary>
        public ICommand DoUploadWork { get; set; }

        public ICommand DoBrowseFiles { get; set; }

        public ICommand SetID3Tags { get; set; }

        #endregion

        #region Data Properties

        public string Name
        {
            get { return "SermonUpload"; }
        }

        /// <summary>
        /// The file browse message to be displayed in the View.
        /// </summary>
        public string SelectedFileText
        {
            get
            {
                if (FtpConnectionSettings.GetInstance().SourceFile.Capacity <= 0 )
                {
                    return "UploadWindowView_TabBar_Browse_Select_Please";
                }
                else
                {
                    return FtpConnectionSettings.GetInstance().SourceFile[0];
                }
            }

            set
            {
                base.RaisePropertyChangingEvent("SelectedFileText");
                FtpConnectionSettings.GetInstance().SourceFile[0] = value;
                base.RaisePropertyChangedEvent("SelectedFileText");
            }
        }

        /// <summary>
        /// Sets the visibility of the browse button.
        /// </summary>
        public Boolean BrowseButtonIsVisible
        {
            get
            {
                return p_BrowseButtonIsVisible;
            }

            set
            {
                base.RaisePropertyChangedEvent("BrowseButtonIsVisible");
                p_BrowseButtonIsVisible = value;
                base.RaisePropertyChangedEvent("BrowseButtonIsVisible");
            }
        }

        /// <summary>
        /// Sets the visibility of the set id3 tag button.
        /// </summary>
        public Boolean SetID3TagIsVisible
        {
            get
            {
                return p_SetID3TagIsVisible;
            }

            set
            {
                base.RaisePropertyChangedEvent("SetID3TagIsVisible");
                p_SetID3TagIsVisible = value;
                base.RaisePropertyChangedEvent("SetID3TagIsVisible");
            }
        }

        /// <summary>
        /// The author text-box in the View.
        /// </summary>
        public string Preacher
        {
            get
            {
                return p_Preacher;
            }

            set
            {
                base.RaisePropertyChangingEvent("Preacher");
                p_Preacher = value;
                base.RaisePropertyChangedEvent("Preacher");
            }
        }

        /// <summary>
        /// The title text-box in the View.
        /// </summary>
        public string SermonTitle
        {
            get
            {
                return p_SermonTitle;
            }

            set
            {
                base.RaisePropertyChangingEvent("SermonTitle");
                p_SermonTitle = value;
                base.RaisePropertyChangedEvent("SermonTitle");
            }
        }

        /// <summary>
        /// The comment text-box in the View.
        /// </summary>
        public string SermonComment
        {
            get
            {
                return p_SermonComment;
            }

            set
            {
                base.RaisePropertyChangingEvent("SermonComment");
                p_SermonComment = value;
                base.RaisePropertyChangedEvent("SermonComment");
            }
        }
 
        /// <summary>
        /// The date text-box in the View.
        /// </summary>
        public DateTime SermonDate
        {
            get
            {
                return p_SermonDate;
            }

            set
            {
                base.RaisePropertyChangingEvent("SermonDate");
                p_SermonDate = value;
                base.RaisePropertyChangedEvent("SermonDate");
            }
        }

        /// <summary>
        /// Sets the visibility of the set id3 tag button.
        /// </summary>
        public Boolean UploadButtonIsEnable
        {
            get
            {
                return p_UploadButtonIsEnable;
            }

            set
            {
                base.RaisePropertyChangedEvent("UploadButtonIsEnable");
                p_UploadButtonIsEnable = value;
                base.RaisePropertyChangedEvent("UploadButtonIsEnable");
            }
        }

        #endregion

        #region Event Invokers

        internal void RaiseFilesSelectedEvent(List<string> SelectedPath)
        {
            FtpConnectionSettings.GetInstance().SourceFile = SelectedPath;
            this.SelectedFileText = SelectedPath[0];

            if (SelectedFileText != null)
            {
                this.BrowseButtonIsVisible = false;
                this.SetID3TagIsVisible = true;
            }
        }

        internal void RaiseID3TageSetEvent()
        {
            if (p_Preacher != null && p_SermonTitle != null)
            {
                this.SetID3TagIsVisible = false;
                this.UploadButtonIsEnable = true;
            }
        }

        /// <summary>
        /// Raises the WorkStarting event.
        /// </summary>
        internal void RaiseWorkStartedEvent()
        {
            // Exit if no subscribers
            if (WorkStarted == null) return;

            if (FtpConnectionSettings.GetInstance().SourceFile == null) return;

            this.UploadButtonIsEnable = false;

            // Raise event
            WorkStarted(this, new EventArgs());
        }

        /// <summary>
        /// Raises the WorkEnding event.
        /// </summary>
        internal void RaiseWorkEndedEvent()
        {
            // Exit if no subscribers
            if (WorkEnded == null) return;

            // Raise event
            WorkEnded(this, new EventArgs());
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Initializes the view model.
        /// </summary>
        private void Initialize()
        {
            // Initialize command properties
            this.DoUploadWork = new DoUploadWorkCommand(this);
            this.DoBrowseFiles = new DoBrowseFilesCommand(this);
            this.SetID3Tags = new SetID3TagsCommand(this);

            // Initialize child view models
            this.ProgressDialogViewModel = new ProgressDialogViewModel(this);
        }

        #endregion
    }
}